import 'package:flutter/material.dart';

/// 分段导航
/// [myTabs]：tab Widget
/// [myTabBarViews]：TabBarView Widget
/// [onSelected]：切换回调

class CustomTabBar extends StatefulWidget {
  final List<Widget> myTabs;
  final List<Widget> myTabBarViews;
  final Function onSelected;
  CustomTabBar(this.myTabs, {this.myTabBarViews, this.onSelected});

  @override
  State<StatefulWidget> createState() {
    return _CustomTabBarState(myTabs, myTabBarViews);
  }
}

class _CustomTabBarState extends State<CustomTabBar>
    with SingleTickerProviderStateMixin {
  List<Widget> _myTabs;
  List<Widget> _myTabBarViews;
  _CustomTabBarState(this._myTabs, this._myTabBarViews);
  TabController _tabController;

  @override
  void initState() {
    _tabController = TabController(length: _myTabs.length, vsync: this);
    _tabController.addListener(() {
      if (widget.onSelected != null) {
        widget.onSelected(_tabController.index);
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        new TabBar(
          tabs: _myTabs,
          indicatorSize: TabBarIndicatorSize.label,
          indicatorColor: Color(0xFF3DBCC6),
          indicatorWeight: 2,
          unselectedLabelColor: Color(0xFF333333),
          labelStyle: new TextStyle(fontSize: 14.0),
          labelColor: Color(0xFF3DBCC6),
          unselectedLabelStyle: new TextStyle(fontSize: 14.0),
          controller: _tabController,
        ),
        Expanded(
            child: TabBarView(
          children: _myTabBarViews,
          controller: _tabController,
        ))
      ],
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
}
